package br.com.salao.ejb.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import br.com.salao.ejb.dao.AgendamentoRatingDao;
import br.com.salao.entity.AgendamentoRating;

public class AgendamentoRatingDaoImpl implements AgendamentoRatingDao {
	
	private Logger logger = Logger.getLogger(AgendamentoRating.class.getName());
	private final GenericDao<AgendamentoRating> dao;
	private EntityManager em;
	
	public AgendamentoRatingDaoImpl(EntityManager em){
		logger.debug("AgendamentoRatingDaoImpl :: construtor");
		this.em = em;
		dao = new GenericDao<AgendamentoRating>(em,AgendamentoRating.class);
	}
	
	public AgendamentoRating inserir(AgendamentoRating agendamentoRating)  throws Exception {
		return dao.salvar(agendamentoRating);
	}

	public AgendamentoRating buscar(int idAgendamentoRating)  throws Exception {
		return dao.buscar(idAgendamentoRating);
	}

	public AgendamentoRating update(AgendamentoRating agendamentoRating)  throws Exception {
		return dao.update(agendamentoRating);
	}
	
	public AgendamentoRating buscarRatingPorAgendamento(int idAgendamento) throws Exception{
		
		try{

			Query query = em.createQuery("from AgendamentoRating a " +
					"where a.agendamento.id = :idAgendamento ");

			query.setParameter("idAgendamento", idAgendamento);

			return (AgendamentoRating) query.getResultList();	
			
		}catch (Exception e) {
			
			StringBuffer sb = new StringBuffer();
			sb.append(e.getStackTrace() + "\n");
			sb.append(e.getCause() + "\n");
			sb.append(e.getMessage() + "\n");

			logger.error(sb.toString(),e);

			e.printStackTrace();			
			throw e;						
		}
	}
	
	public List<AgendamentoRating> buscarRatingPorEstabelecimento(int idEstabelecimento) throws Exception{
		
		try{

			Query query = em.createQuery("from AgendamentoRating a " +
					"where a.agendamento.estabelecimento.id = :idEstabelecimento ");

			query.setParameter("idEstabelecimento", idEstabelecimento);

			return (List<AgendamentoRating>) query.getResultList();	
			
		}catch (Exception e) {
			
			StringBuffer sb = new StringBuffer();
			sb.append(e.getStackTrace() + "\n");
			sb.append(e.getCause() + "\n");
			sb.append(e.getMessage() + "\n");

			logger.error(sb.toString(),e);

			e.printStackTrace();			
			throw e;						
		}
	}	

}
